require 'learn_scraper'
require 'rexml/document'

class WebScraper 
  def get_mailbox_types
    mailbox_types = {}
    xml = REXML::Document.new(File.open("mailbox_types.xml"))
    xml.elements.each("//type") do |type|
      scraper_name = type.elements["scraper"].text
      mailbox_types[type.elements["id"].text] = scraper_name
      require scraper_name.to_lowercase_underscore
    end
    mailbox_types
  end
  
  def scrape_mailboxes(mailbox_accounts)
    mailbox_types = get_mailbox_types
    mailboxes = []
    mailbox_accounts.each do |mailbox_account|   
      begin
        mails = Class.by_name(mailbox_types[mailbox_account[:type]]).scrape(mailbox_account[:username], mailbox_account[:password])
        mailboxes << {
          :username => Iconv.conv("UTF-8", "GBK", mailbox_account[:username]),
          :type => Iconv.conv("UTF-8", "GBK", mailbox_account[:type]),
          :state => "ok",
          :mails => mails,
        }
      rescue Exception => e
        mailboxes << {
          :username => Iconv.conv("UTF-8", "GBK", mailbox_account[:username]),
          :type => Iconv.conv("UTF-8", "GBK", mailbox_account[:type]),
          :state => e.message,
          :mails => [],
        }
      end
    end
    mailboxes
  end
  
  def scrape_courses(username, password)
    LearnScraper.scrape(username, password)
  end  
end



